// util.js

// 引入其他包
import { Promise } from '../libs/rsvp-latest.min';
import netUtil from "netUtil.js";
import uiUtil from "uiUtil.js";

// 登录
function login() {
    return new Promise(function (resolve, reject) {
        wx.login({
            success: function (res) {
                resolve(res);
            },
            fail: function (res) {
                reject(res);
            },
        })
    });
}

// 获取用户信息
function getUserInfo() {
    return new Promise(function (resolve, reject) {
        wx.getUserInfo({
            success: function (res) {
                resolve(res);
            },
            fail: function (res) {
                reject(res);
            },
        });
    });
}


// 获取openId 和 session_key
function getOpenIdAndKey(code) {
    let Config = getApp().Config;
    return new Promise(function (resolve, reject) {
        wx.request({
            url: `${Config.HOST}?r=applet/getopenid`,
            data: {
                js_code: code,
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                resolve(res);
            },
            fail: function (res) {
                reject(res);
            },
        })
    })
}



// 获取用户学号
function getStNumberPassword(openId) {
    let Config = getApp().Config;
    return new Promise(function (resolve, reject) {
        wx.request({
            url: `${Config.HOST}?r=user/getnupabyopenid`,
            data: {
                openId: openId,
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                resolve(res.data);
            },
            fail: function (res) {
                reject(res);
            },
        })
    })
}

// 判断是否绑定
function checkBind(res) {
    let Config = getApp().Config;
    return new Promise(function (resolve, reject) {
        if (res.error == Config.ERR.ER) {
            uiUtil.goToBindUser();
        } else {
            resolve(res);
        }
    })
}


module.exports = {
    "NET": netUtil,
    "UI": uiUtil,
    "login": login,
    "getUserInfo": getUserInfo,
    "getOpenIdAndKey": getOpenIdAndKey,
    "getStNumberPassword": getStNumberPassword,
    "checkBind":checkBind,
}

